package com.tattermedia.allthataudio.audio;

import hw.ats.news.R;

import com.tattermedia.allthataudio.misc.TimeFomatter;
import com.tattermedia.allthataudio.views.AudioPlayerProgressView;

import android.media.AudioManager;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup.MarginLayoutParams;
import android.view.animation.Animation;
import android.view.animation.TranslateAnimation;
import android.widget.ImageButton;
import android.widget.TextView;

// common audio player status listener
public class CommonAudioPlayerStatusListener implements
		IAudioPlayerStatusListener {
	
	private ImageButton 	mPlayButton;
	private ImageButton		mRepeatButton;
	private ImageButton		mPrevButton;
	private ImageButton		mNextButton;
	private TextView		mTxSubject;
	private TextView		mTxTime;
	private AudioPlayerProgressView		mProgressView;
	
	public CommonAudioPlayerStatusListener(ImageButton playButton, 
								ImageButton repeatButton, 
								ImageButton prevButton,
								ImageButton nextButton,
								TextView textSubject,
								TextView textTime,
								AudioPlayerProgressView progressView) {
		mPlayButton = playButton;
		mRepeatButton = repeatButton;
		mPrevButton = prevButton;
		mNextButton = nextButton;
		mTxSubject = textSubject;		
		mTxTime = textTime;
		mProgressView = progressView;
		
		
		mPlayButton.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				onPlayButtonClicked();
			}
		});
		
		mRepeatButton.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				onRepeatButtonClicked();				
			}
		});
		
		mPrevButton.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				try {
					AudioPlayerManager.getManager().playPrevAudio();
				} catch (Exception e) {
					e.printStackTrace();
				}				
			}
		});

		mNextButton.setOnClickListener(new OnClickListener() {
		
			@Override
			public void onClick(View v) {
				try {
					AudioPlayerManager.getManager().playNextAudio();
				} catch (Exception e) {
					e.printStackTrace();
				}				
			}
		});
		
	}

	@Override
	public void onAudioStatusChanged(int type) {
		AudioPlayerManager manager = AudioPlayerManager.getManager();
		
		if (manager.isPlaying()) {
			mPlayButton.setImageResource(R.drawable.audio_player_button_pause);
			mPlayButton.setOnClickListener(new OnClickListener() {
				
				@Override
				public void onClick(View v) {
					onPauseButtonClicked();					
				}
			});
			
		} else {
			mPlayButton.setImageResource(R.drawable.audio_player_button_play);
			
			mPlayButton.setOnClickListener(new OnClickListener() {
				
				@Override
				public void onClick(View v) {
					onPlayButtonClicked();
				}
			});
		}
		
		if (manager.getRepeatMode() == AudioPlayerManager.REPEAT_ALL) {
			mRepeatButton.setImageResource(R.drawable.audio_player_button_repeat);
		} else {
			mRepeatButton.setImageResource(R.drawable.audio_player_button_repeat_one);
		}
		
		
		PlayListItem item = manager.getCurrentPlayingItem();
		
		// set subject
		if (item != null && !item.getTitle().equals(mTxSubject.getText())) {
			mTxSubject.setText(item.getTitle());
		}
		
		// set time
		mTxTime.setText(TimeFomatter.format(manager.getCurrentPosition()) + "/" + TimeFomatter.format(manager.getTotalDuration()));
		
		
		mProgressView.invalidate();
	}
	
	protected void onPlayButtonClicked() {
		try {
			AudioPlayerManager.getManager().resumeAudio();
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	
	protected void onPauseButtonClicked() {
		AudioPlayerManager.getManager().pauseAudio();
	}
	
	protected void onRepeatButtonClicked() {
		AudioPlayerManager.getManager().toggleRepeat();
	}

}
